package com.codi.bus.core.service.impl;

import com.codi.bus.core.dao.CdUserShareDao;
import com.codi.bus.core.dao.PortfolioTradeAccoDao;
import com.codi.bus.core.domain.CdUserShare;
import com.codi.bus.core.domain.PortfolioTradeAcco;
import com.codi.bus.core.service.UserShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lvgo
 * @version 1.0
 * @Description: 用户份额明细-本地数据
 * @date 18-6-15 下午3:02
 */
@Service
public class UserShareServiceImpl implements UserShareService {

    @Autowired
    private CdUserShareDao userShareDao;

    @Autowired
    private PortfolioTradeAccoDao portfolioTradeAccoDao;


    @Override
    public List<CdUserShare> selectPortfolioSharesByUserId(Long userId) {
        ArrayList<CdUserShare> cdUserShares = new ArrayList<>();
        List<PortfolioTradeAcco> portfolioTradeAccos = portfolioTradeAccoDao.selectPortfolioTradeAccoByUserId(userId);
        for (PortfolioTradeAcco portfolioTradeAcco : portfolioTradeAccos) {
            List<CdUserShare> userShares = userShareDao.selectByUserIdAndTradeAcco(userId, portfolioTradeAcco.getPortfolioTradeAcco());
            cdUserShares.addAll(userShares);
            userShares.clear();
        }
        return cdUserShares;
    }

    @Override
    public List<CdUserShare> selectPortfolioSharesByUserId(Long userId, String portfolioCode) {
        ArrayList<CdUserShare> cdUserShares = new ArrayList<>();
        List<PortfolioTradeAcco> portfolioTradeAccos = portfolioTradeAccoDao.selectPortfolioTradeAccoByUserId(userId);
        portfolioTradeAccos = portfolioTradeAccos.stream().filter(portfolioTradeAcco -> portfolioCode.equals(portfolioTradeAcco.getPortfolioCode())).collect(Collectors.toList());
        for (PortfolioTradeAcco portfolioTradeAcco : portfolioTradeAccos) {
            List<CdUserShare> userShares = userShareDao.selectByUserIdAndTradeAcco(userId, portfolioTradeAcco.getPortfolioTradeAcco());
            cdUserShares.addAll(userShares);
            userShares.clear();
        }
        return cdUserShares;
    }
}
